import wepy from 'wepy'

export default class extends wepy.mixin {
  data = {
    address: null,
    cart: []
  }

  methods = {
    // 选择收货地址
    async chooseAddress () {
      const res = await wepy.chooseAddress().catch(err => err)
      if (res.errMsg !== 'chooseAddress:ok') return wepy.baseToast('取消选择地址')
      wepy.baseToast('已选择地址')
      this.address = res
      wepy.setStorageSync('address',res)
      this.$apply()
    },
    // 
    async getWxUser (e) {
      const { encryptedData, iv, rawData, signature } = e.detail
      const {code} = await wepy.login()
      console.log(encryptedData, iv, rawData, signature)
      console.log(code)
    }
  }
  onLoad (options) {
    this.address = wepy.getStorageSync('address') || null
    this.cart = this.$parent.globalData.cart.filter(item => {
      return item.isCheck === true
    })
  }
  computed = {
    // 控制是否显示选择收货地址
    isShowChooseAddress () {
      return this.address === null
    },
    // 收货地址
    addressStr () {
      if (this.address === null) return '请选择收货地址'
      const {provinceName, cityName, countyName, detailInfo} = this.address
      return provinceName + cityName + countyName + detailInfo
    }
  }

  onShow () {
  }
}
